package com.itlaoqi.thread.juc;

import java.util.concurrent.*;

public class FutruesSample {

    public static void main(String[] args) {
        ExecutorService threadPool = Executors.newFixedThreadPool(10);
        for (int i = 0; i < 10000; i++) {
            Computor c = new Computor();
            c.setNum(i);
            //future是对计算的线程进行监听,因为计算是在其他的线程是进行的，所以计算过程是异步的。
            Future<Boolean> result = threadPool.submit(c);
            try {
                Boolean r =  result.get();//用于获取返回值，如果线程内部的call没有执行完成，则进入等待状态。直到计算完成
                if(r){
                    System.out.println(c.getNum());
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
        threadPool.shutdown();
    }
}

class Computor implements Callable<Boolean>{
    private Integer num;
    @Override
    public Boolean call() throws Exception {
        boolean isprime = true;
        for (int i = 2; i < num; i++) {
            if(num % i == 0){
                isprime = false;
                break;
            }
        }
        return isprime;
    }

    public Integer getNum() {
        return num;
    }

    public void setNum(Integer num) {
        this.num = num;
    }
}
